package com.ruoyi.web.controller.wexin;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.weixin.Exception.BussnissExecption;
import com.ruoyi.weixin.anno.ILog;
import com.ruoyi.weixin.anno.RequireLogin;
import com.ruoyi.weixin.entity.*;
import com.ruoyi.weixin.service.*;
import com.ruoyi.weixin.untils.OssUtil;
import com.ruoyi.weixin.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * (Pictures)表控制层
 *
 * @author makejava
 * @since 2023-12-26 09:51:50
 */

@RestController
@RequestMapping("/wexinLogin/pic")
public class PictureController {

    @Autowired
    UserService userService;
    @Resource
    private PicturesService picturesService;
    @Resource
    PictureService pictureService;
    @Resource
    PicUrlService picUrlService;
    @Autowired
    MinioService minioService;
    @GetMapping("/minio")
    public  List<String> getList() {
        List<String> users = userService.list().stream().map(item -> item.getPicUrl()).collect(Collectors.toList());
        List<String> frents = picUrlService.list().stream().map(item -> item.getPicUrl()).collect(Collectors.toList());
        List<String> prcurls = pictureService.list().stream().map(item -> item.getPicUrl()).collect(Collectors.toList());
        prcurls.addAll(frents);
        prcurls.addAll(users);
        List<String> ossUrls = minioService.listObjectNames("xingyue");


        System.err.println(ossUrls);
        System.err.println(prcurls.size()+"-----------"+ossUrls.size());

        List<String> finallyList = new ArrayList<>();
        for (String ossUrl : ossUrls) {
            if (!prcurls.contains(ossUrl)) {
                finallyList.add(ossUrl);
                Pattern pattern = Pattern.compile("xingyue/(.*)");
                Matcher matcher = pattern.matcher(ossUrl);
                if (matcher.find()) {
                    String desiredPart = matcher.group(1);
                    System.err.println(desiredPart);
                    minioService.removeObject("xingyue", desiredPart);
                }
            }
        }
        System.err.println(finallyList);
        return  finallyList;
    }

    @GetMapping("/oss")
    public  List<String> getListtest() {
        List<String> users = userService.list().stream().map(item -> item.getPicUrl()).collect(Collectors.toList());
        List<String> frents = picUrlService.list().stream().map(item -> item.getPicUrl()).collect(Collectors.toList());
        List<String> prcurls = pictureService.list().stream().map(item -> item.getPicUrl()).collect(Collectors.toList());
        prcurls.addAll(frents);
        prcurls.addAll(users);
        List<String> ossUrls = OssUtil.getObjList();
        ArrayList<String> hostUrls = new ArrayList<>();
        for (String pic : prcurls) {
            String result = null;
            Pattern pattern = Pattern.compile("com/(.*)");
            Matcher matcher = pattern.matcher(pic);
            if (matcher.find()) {
                result = matcher.group(1);
                if (result.startsWith("HongMeng")) {
                    hostUrls.add(result);
                }
            } else {
                System.out.println("No match found");
            }
        }
        for (String user : users) {
            if (user.startsWith("HongMeng")) {
                hostUrls.add(user);
            }
        }
        System.err.println(hostUrls);
        System.err.println("------------------------------------------");
        System.err.println(ossUrls);
        System.err.println(hostUrls.size()+"-----------"+ossUrls.size());
        List<String> finallyList = new ArrayList<>();
        for (String ossUrl : ossUrls) {
            if (!hostUrls.contains(ossUrl)) {
                finallyList.add(ossUrl);
                OssUtil.delete(ossUrl);
            }
        }
        System.err.println(finallyList);
        return  finallyList;
    }


    @PostMapping("findLajiAndDelete")
    public R findLajiAndDelete() {
        return R.ok(pictureService.findLajiAndDelete());
    }


    @PostMapping("deletePics")
    @ILog("删除图片")
    public R deletePics(String id) {
        return R.ok(pictureService.deletePics(id));
    }


    @RequireLogin
    @PostMapping("movePic")
    public R movePic(String id, String name) {
        return R.ok(pictureService.movePic(id, name));
    }

    @PostMapping("orderList")
    public R orderList(@RequestBody orderVo ordervo) {
        ordervo.getPictureList().forEach(System.out::println);
        picturesService.updateBatchById(ordervo.getPictureList());
        return R.ok();
    }

    @PostMapping("delete")
    public R delete(String id, String userId) {
        return R.ok(pictureService.removeByid(id, userId));
    }

    @PostMapping("reset")
    public R reset(String id, String userId) {
        return R.ok(pictureService.reset(id, userId));
    }


    @PostMapping("NewloadPics")
    public R uploadPic(@RequestBody Frents frents) {
        return R.ok(picturesService.NewloadPics(frents));
    }

    @PostMapping("newPrcs")
    public R newPrcs() {
        return R.ok(picturesService.newPrcs());
    }

    @PostMapping("getUserByPhone")
    public R getUserByPhone(String phone) {
        User one = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getPhone, phone));
        if (one == null) {
            throw new BussnissExecption("请联系好友完善信息");
        }
        return R.ok(one);
    }

    @RequireLogin
    @ILog("新建相册")
    @PostMapping("newPicss")
    public R newPicss(@RequestBody PicturesVo pictures) {
        return R.ok(picturesService.newPicss(pictures));
    }


    @PostMapping("getPics")
    public R getPics(String userId) {
        List<Pictures> list = picturesService.list(new LambdaQueryWrapper<Pictures>().eq(Pictures::getUserId, userId).eq(Pictures::getStatus, 0));
        List<Pictures> pics = picturesService.getPics(userId);
        pics.addAll(list);
        return R.ok(pics.stream().map(item -> item.getTypeName()).collect(Collectors.toList()));
    }

    @RequireLogin
    @PostMapping("newPi")
    public R newPi(@RequestBody Picture picture, String userId) {
        return R.ok(picturesService.newPi(picture, userId));
    }

    @RequireLogin
    @PostMapping("checkPicName")
    public R checkPicName(String nickName) {
        return R.ok(picturesService.checkPicName(nickName));
    }

    @RequireLogin
    @PostMapping("count")
    public R count(String userId) {
        return R.ok(picturesService.accountcount(userId));
    }

}

